﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FTchina;
using System.Data;
using System.Collections;

public partial class UI_usergroup : System.Web.UI.Page
{
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        basic.CheckAdmin(this);
        basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());

        if (!IsPostBack)
        {
            bindData();
            // bindPowerType();
            if (Request["action"] == "xiugai") switchtype();

        }
    }

    private void switchtype()
    {

        txbcontent.Text = shujuku.GetValueByKey("groupname,id", "usergroup", "id=" + Request["id"].ToString());
        //取出用户组角色
        string[] powers = shujuku.GetValueByKey("power,id", "usergroup", "id=" + Request["id"].ToString()).ToString().Split(',');
        if (powers.Length > 0)
        {
            ArrayList alpower = new ArrayList();
            for (int i = 0; i < powers.Length; i++)
            {
                alpower.Add(powers[i].ToString());

            }
            //for (int i = 0; i < chkpw.Items.Count; i++)
            //{
            //    if (alpower.IndexOf(chkpw.Items[i].Value) != -1)
            //    {
            //        chkpw.Items[i].Selected = true;
            //    }
            //}
        }
        btnok.Text = "确定修改";
    }


    //void bindPowerType()
    //{
    //    chkpw.DataSource = shujuku.GetDataSet("select * from powertype");
    //    chkpw.DataTextField = "powertype";
    //    chkpw.DataBind();
    //}
    void bindData()
    {
        string GetDateSql = "select * from usergroup  order by id desc";
        ds = shujuku.GetDataSet(GetDateSql);
        gvList.DataSource = ds;
        gvList.DataBind();
        LblCurrentIndex.Text = (gvList.PageIndex + 1).ToString();
        LblPageCount.Text = gvList.PageCount.ToString();
        LblRecordCount.Text = ds.Tables[0].Rows.Count.ToString();
        if (ds.Tables[0].Rows.Count == 0)
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;

            LblCurrentIndex.Visible = false;
            LblPageCount.Visible = false;
            LblRecordCount.Visible = false;
            Label1.Visible = false;
            Label1.Visible = false;
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;

            //lbMessage.Text = "没有记录！";
        }
        else if (gvList.PageCount == 1)
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;
        }
        // 计算生成分页页码,分别为："首 页" "上一页" "下一页" "尾 页"
        btnFirst.CommandName = "1";
        btnPrev.CommandName = (gvList.PageIndex == 0 ? "1" : gvList.PageIndex.ToString());
        btnNext.CommandName = (gvList.PageCount == 1 ? gvList.PageCount.ToString() : (gvList.PageIndex + 2).ToString());
        btnLast.CommandName = gvList.PageCount.ToString();

    }
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        gvList.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        bindData();
    }
    protected void gvList_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        if (gvList.PageCount > 1)
        {
            gvList.PageIndex = e.NewPageIndex;
            bindData();
        }
        else
        {
            LblCurrentIndex.Text = gvList.EditIndex.ToString();
        }
    }
 


    protected void btnok_Click(object sender, EventArgs e)
    {
        string content = basic.SafeRequest(txbcontent.Text);
        if (content.Length == 0) { JS.Alert("请填写名称"); return; }

        string Sql = null;

        //取权限的数组
        string power = string.Empty;

        //foreach (ListItem item2 in chkpw.Items)
        //{
        //    if (item2.Selected)
        //    {
        //        power = power + item2.Text.ToString() + ",";
        //    }
        //}
        //if (power.Length > 1)
        //{
        //    power = power.Substring(0, power.Length - 1);
        //}

        string ChkGroupNamesql = "select id from usergroup where groupname='" + content.Trim() + "'";
        if (shujuku.testread(ChkGroupNamesql))
        {
            JS.Alert("已经有此名称存在了。");
            return;
        }

        if (btnok.Text == "确定修改")
        {
            Sql = "update usergroup set groupname='" + content + "',power='" + power + "' where id=" + Request["id"];
        }
        else
        {
            Sql = "insert into usergroup (groupname,power) values ('" + content + "','" + power + "')";
        }
        UImsg.PageRender(shujuku.ExecuteSql(Sql));


    }




    /// <summary>
    /// 检查看看是否能有关联，菜单有关联不能删除，人员有关联不能删除
    /// </summary>
    /// <param name="groupId"></param>
    /// <returns>true 有关联 false 无关联</returns>
    public Boolean ChkRefered(string groupId)
    {
        bool flag = false;
        string MenuListSql = "select * from menulist where power like '%" + groupId + "%' order by id desc";
        //basic.Log(MenuListSql);
        foreach (DataRow dr in shujuku.GetDataTable(MenuListSql).Rows)
        {
            if (dr["power"].ToString().Trim().Length > 0)
            {
                flag = basic.ChkISinArray(groupId, basic.String2ArrayList(dr["power"].ToString()));
                
                if (flag)
                {
                    break;
                }
            }

        }
        if (!flag)
        { //mennlist 没有关联了
            string MemberSql = "select * from master where power ='" + groupId + "' order by id desc";
            flag = shujuku.testread(MemberSql);
           // basic.Log("MemberSql" + flag);
        }

       // basic.Log(groupId + flag);
        return flag;

    }
}
